package net.telnet;

import org.apache.commons.net.telnet.TelnetClient;




import interf.ITelnetHandler;




import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.SocketTimeoutException;

public class SocketClient {
    private TelnetClient telnet = null;
    public TelnetClient getTelnet() {
		return telnet;
	}

	public void setTelnet(TelnetClient telnet) {
		this.telnet = telnet;
	}

	private String TelType = "VT220";

    public String host;
    public int port;
    BufferedReader br = null;
    PrintWriter pw = null;
    
    public ITelnetHandler hand = null;
    
    
    private Thread thread;


    public SocketClient(ITelnetHandler h) {
    	hand = h;
    }

    public void sendMsg(Object o) {
        if (telnet != null && telnet.isConnected() && pw != null) {
            pw.print(o + "\n");
            pw.flush();
        }
    }
    
    //留给外部检查
    public TelnetClient getTelnetObj(){
    	return telnet;
    }
    

    public void connect() {
        thread = new Thread(new Runnable() {
            
            public void run() {
                telnet = new TelnetClient(TelType);
                try {
                    telnet.setConnectTimeout(5000);
                    telnet.connect(host, port);
                   
                    br = new BufferedReader(new InputStreamReader(telnet.getInputStream(), "gbk"));
                    pw = new PrintWriter(new OutputStreamWriter(telnet.getOutputStream(), "gbk"));
                    String content;
                    
                    while(true){
                    	
                    	//!thread.isInterrupted() && 
                    	/*
                    	if ( thread.isInterrupted() ){
                    		 hand.OnRecvComplete(MainHandler.SERVER_CONNECT_ERR);
                    		 Thread.interrupted();
                    		 break;
                    	}
                    	*/
                    	if (thread!=null && !thread.isInterrupted() && (content = br.readLine()) != null){
                    		hand.OnRecvComplete(content);
                    		continue;
                    	}
                    	//报错后 通知外部，外部直接stop连接..
	                   	//hand.OnRecvComplete(MainHandler.SERVER_CONNECT_ERR);
	                   	break;
                    	
                    }
                    
                   
                    //while (!thread.isInterrupted() && (content = br.readLine()) != null) {
                    /*
                    while (!thread.isInterrupted() && (content = br.readLine())!=null ) {
                    	
                    	if (telnet.isConnected()){
                    		
                    		hand.OnRecvComplete(content);
                    		
                    	}
                    	
                    	
                    	if (!telnet.isConnected()){
                    		hand.OnRecvComplete(MainHandler.SERVER_CONNECT_ERR);
                    	    Thread.interrupted();
                    	    break;
                    	}
                    	
                    	if ((content = br.readLine())!=null){
                    		hand.OnRecvComplete(content);
                    	}
                    
                    	
                    }*/
                } catch (SocketTimeoutException e) {
                } catch (IOException io) {
                    io.printStackTrace();
                    //hand.OnRecvComplete(MainHandler.SERVER_CONNECT_ERR);
                }
            }
        });
        thread.start();
    }

    public void stop() {
        try {
            if (pw != null) {
                pw.close();
            }
            if (br != null)
                br.close();
            if (thread != null) {
                thread.isInterrupted();
                thread = null;
               // thread.destroy();
               
            }
            if (telnet != null) {
                telnet.disconnect();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void start() {
        if (thread != null)
            stop();
        connect();
    }

}
